<template>
	<app-layout>
		<view class="user-top">
			<view class="top">
				<view class="avatar">
					<image class="img" :src="uInfo.userInfo.avatar" mode=""></image>
				</view>
				<view class="news">
					<view class="user">
						<view class="follow" @click="follow">
							<text class="text">{{foucesnum}}</text>关注
						</view>
						<view class="fans" @click="fans">
							<text class="text">{{fansnum}}</text>粉丝
						</view>
					</view>
					<view @click="changFollow" class="attention addten" v-if="is_own != 1 && is_focus == 0">
						+ 关注
					</view>
					<view @click="cancel" class="attention remten" v-if="is_own != 1 && is_focus == 1">
						取消关注
					</view>
				</view>
			</view>
			<view class="nickname">
				{{uInfo.nickname}}
			</view>
			<view class="introduce">
				<!-- 10.22 修改 添加编辑 -->
				<text class="in-text">{{vidianinfo.description ? vidianinfo.description : '暂无个人介绍'}}</text>
				<text class="edit" @click="edit" v-if="is_own == 1">编辑</text>
			</view>
			<view class="info">
				<view class="gender">
					{{uInfo.sex}}
				</view>
				<view class="address" v-if="uInfo.userInfo.city != ''">
					{{uInfo.userInfo.country}} · {{uInfo.userInfo.province}} · {{uInfo.userInfo.city}}
				</view>
			</view>
			<!-- 9.8 修 · 只可看自己的喜欢和收藏 -->
			<view class="three" :class="isTop == 0 ? '' : 'threefix'">
				<view class="flex">
					<view class="item" @click="changeopera(item.id)" :class="operaid == item.id ? 'active' : ''"
						v-for="(item,index) in operalist" :key="index" v-if="is_own != 1 ? index == 0 : index > -1">
						{{item.title}}
					</view>
				</view>
				<!-- <view class="">
					作品
				</view>
				<view class="">
					喜欢
				</view>
				<view class="">
					收藏
				</view> -->
			</view>

		</view>
		<!-- <view class="" :style="{'margin-top': isTop == 0 ? '' : '120rpx'}">
			<xyz-masonry :prevent="prevent" :isSize="false" :borderRadius="16" :imgWidth="351" :masonryList="selflist"
				@itemClick="itemClick" @click="del" @delik="delik" @decolk="decolk" :isdel="isdel" :isdelup="isdelup"
				:isdelcol="isdelcol" :operaid="operaid" />
		</view> -->
		<!-- 9.8 修 * 点击可进入上下滑动页面 作品，喜欢，收藏 -->
		<!-- 9.24 修改组件后 -->
		<view class="" v-if="operaid == 0" :style="{'margin-top': isTop == 0 ? '' : '120rpx'}">
			<xyz-masonry @itemClick="itemClick" :value="workslist" :operaid="operaid" @click="del" :isdel="isdel" />
		</view>
		<view class="" v-if="operaid == 1" :style="{'margin-top': isTop == 0 ? '' : '120rpx'}">
			<xyz-masonry @itemClick="itemClick" :value="likelist" :operaid="operaid" @delik="delik"
				:isdelup="isdelup" />
		</view>
		<view class="" v-if="operaid == 2" :style="{'margin-top': isTop == 0 ? '' : '120rpx'}">
			<xyz-masonry @itemClick="itemClick" :value="collist" :operaid="operaid" @decolk="decolk"
				:isdelcol="isdelcol" />
		</view>
		<view class="nocenter" v-if="selflist.length == 0">
			<view class="" v-if="operaid == 0">
				{{workstitle}}
			</view>
			<view class="" v-if="operaid == 1">
				{{liketitle}}
			</view>
			<view class="" v-if="operaid == 2">
				{{colltitle}}
			</view>
		</view>
	</app-layout>
</template>

<script>
	import {
		mapState
	} from "vuex";
	import xyzMasonry from '../components/xyz-masonry.vue';
	let totop = 0
	export default {
		data() {
			return {
				list: [],
				operalist: [{
						id: 0,
						title: '作品'
					},
					{
						id: 1,
						title: '喜欢'
					},
					{
						id: 2,
						title: '收藏'
					}
				],
				operaid: 0,

				user_id: 0,
				uInfo: '',
				vidianinfo: '',
				fansnum: 0,
				foucesnum: 0,
				is_focus: 0,
				is_own: 0,

				page: 1,
				limit: 20,
				pagecount: 1,
				selflist: [],

				workslist: [],
				likelist: [],
				collist: [],

				target_type: 1,
				isdel: false,
				isdelup: false,
				isdelcol: false,

				isTop: 0,
				// totop: 0,

				prevent: false,

				workstitle: '正在加载中',
				liketitle: '正在加载中',
				colltitle: '正在加载中',
			}
		},
		mounted() {
			const query = uni.createSelectorQuery().in(this);
			query.select('.three').boundingClientRect(data => {
				totop = data.top
			}).exec();
		},
		components: {
			xyzMasonry
		},
		onPageScroll: function(e) {
			if (e.scrollTop > totop) {
				this.isTop = 1
			} else {
				this.isTop = 0
			}
		},
		onLoad(options) {
			if (options.user_id) {
				this.user_id = options.user_id
			} else {
				this.user_id = this.userInfo.options.user_id
			}
			this.initLoad()
			if (this.user_id == this.userInfo.options.user_id) {
				this.isdel = true
				this.isdelup = false
				this.isdelcol = false
			}
			this.getSelfList()
		},
		// 11.15 每次编辑个人信息成功刷新
		onShow() {
			let hi = uni.getStorageSync('haiwolaile')
			if (hi == 'hi') {
				this.initLoad()
				// this.$store.dispatch('user/config');
				// this.$store.dispatch('user/info');
				uni.removeStorageSync('haiwolaile')
			}
		},
		onReachBottom() {
			if (this.pagecount > this.page) {
				this.page++
				if (this.operaid == 0) {
					if (this.user_id == this.userInfo.options.user_id) {
						this.isdel = true
						this.isdelup = false
						this.isdelcol = false
					}
					this.getSelfList()
				} else if (this.operaid == 1) {
					if (this.user_id == this.userInfo.options.user_id) {
						this.isdel = false
						this.isdelup = true
						this.isdelcol = false
					}
					this.getLikeList(0)
				} else if (this.operaid == 2) {
					if (this.user_id == this.userInfo.options.user_id) {
						this.isdel = false
						this.isdelup = false
						this.isdelcol = true
					}
					this.getLikeList(1)
				}
			}
		},
		computed: {
			...mapState({
				userInfo: state => state.user.info
			}),
		},
		methods: {
			// 关注
			changFollow() {
				if (this.$store.state.user.accessToken === '' || this.$store.state.user.accessToken ===
					null) {
					this.$store.dispatch('user/accessToken');
					return;
				}
				this.addFollow();
			},
			cancel() {
				this.$request({
					url: this.$api.sixty.delfollow,
					data: {
						target_id: this.user_id
					},
					method: 'post'
				}).then(res => {
					if (res.code == 1) {
						this.is_focus = 0
						this.fansnum--
						uni.showToast({
							icon: 'none',
							title: '已取消关注'
						})
					}
				}).catch(err => {
					if (err.msg) {
						uni.showToast({
							icon: 'none',
							title: err.msg
						})
					}
					// console.log(err);
				})
			},
			addFollow() {
				let that = this
				that.$request({
					url: that.$api.sixty.addfollow,
					data: {
						target_id: that.user_id
					},
					method: 'post'
				}).then(res => {
					if (res.code == 0) {
						that.is_focus = 1
						that.fansnum++
						uni.showToast({
							title: res.msg,
							icon: 'none'
						});
					}
				}).catch(err => {
					if (err.msg) {
						uni.showToast({
							icon: 'none',
							title: err.msg
						})
					}
					// console.log(err);
				})
			},
			changeopera(id) {
				let that = this
				if (that.operaid == id) {
					return;
				}
				// that.prevent = true
				that.operaid = id
				that.selflist = []

				that.workslist = []
				that.likelist = []
				that.collist = []

				that.page = 1
				that.pagecount = 1
				if (id == 0) {
					// that.$utils.throttle(function() {}, 500)
					if (that.user_id == that.userInfo.options.user_id) {
						that.isdel = true
						that.isdelup = false
						that.isdelcol = false
					}
					that.getSelfList()
				} else if (id == 1) {
					if (that.user_id == that.userInfo.options.user_id) {
						that.isdel = false
						that.isdelup = true
						that.isdelcol = false
					}
					that.getLikeList(0)
				} else if (id == 2) {
					if (that.user_id == that.userInfo.options.user_id) {
						that.isdel = false
						that.isdelup = false
						that.isdelcol = true
					}
					that.getLikeList(1)
				}

			},
			itemClick(id, likeid) {
				// console.log(id);
				// 9.8 注释
				// uni.navigateTo({
				// 	url: `/pages/sixty/fullscreen/editdex?id=${id}`,
				// })
				// 9.8 修 均可跳转喜欢收藏
				uni.navigateTo({
					url: `/pages/sixty/fullscreen/index?id=${likeid}&userid=${this.user_id}&targetype=${this.operaid}`,
				})
			},
			del(id) {
				this.$request({
					url: this.$api.sixty.delete,
					method: 'post',
					data: {
						id: id,
						user_id: this.userInfo.options.user_id
					},
				}).then(res => {
					if (res.code == 0) {
						this.selflist = []

						this.workslist = []

						this.page = 1
						this.pagecount = 1
						this.getSelfList()
					}
				}).catch(err => {
					if (err.msg) {
						uni.showToast({
							icon: 'none',
							title: err.msg
						})
					}
					// console.log(err);
				})
			},
			// 取消点赞
			delik(id) {
				this.$request({
					url: this.$api.sixty.delike,
					method: 'post',
					data: {
						video_id: id,
					},
				}).then(res => {
					if (res.code == 1) {
						this.selflist = []

						this.likelist = []

						this.page = 1
						this.pagecount = 1
						this.getLikeList(0)
					}
				}).catch(err => {
					if (err.msg) {
						uni.showToast({
							icon: 'none',
							title: err.msg
						})
					}
					// console.log(err);
				})
			},
			// 取消收藏
			decolk(id) {
				this.$request({
					url: this.$api.sixty.delcollect,
					method: 'post',
					data: {
						video_id: id,
						// user_id: this.userInfo.options.user_id
					},
				}).then(res => {
					if (res.code == 1) {
						this.selflist = []

						this.collist = []

						this.page = 1
						this.pagecount = 1
						this.getLikeList(1)
					}
				}).catch(err => {
					if (err.msg) {
						uni.showToast({
							icon: 'none',
							title: err.msg
						})
					}
					// console.log(err);
				})

			},
			getSelfList() {
				let adata = ''
				if (this.user_id == this.userInfo.options.user_id) {
					adata = {
						page: this.page,
						page_size: this.limit,
						user_id: this.user_id
					}
				} else {
					adata = {
						page: this.page,
						page_size: this.limit,
						user_id: this.user_id,
						review_status: 1
					}
				}
				this.$request({
					url: this.$api.sixty.list,
					data: adata
				}).then((res) => {
					// console.log(res);
					this.pagecount = res.data.pagination.page_count
					this.selflist = this.selflist.concat(res.data.list);
					this.workslist = this.workslist.concat(res.data.list);

					if (this.workslist.length == 0) {
						this.workstitle = '暂无上传作品'
					}
					if (res.code != 0) {
						this.workstitle = '暂无上传作品'
					}
					// this.$storage.setStorageSync('agetvideo', this.list)
				}).catch(err => {
					this.workstitle = '暂无上传作品'
					if (err.msg) {
						uni.showToast({
							icon: 'none',
							title: err.msg
						})
					}
					// console.log(err);
				})
			},
			getLikeList(target_type) {
				this.$request({
					url: this.$api.sixty.falist,
					data: {
						page: this.page,
						page_size: this.limit,
						target_type: target_type,
						target_id: this.user_id,
						review_status: 1
					}
				}).then((res) => {
					// console.log(res);
					this.pagecount = res.data.pagination.page_count
					this.selflist = this.selflist.concat(res.data.list);
					if (target_type == 0) {
						this.likelist = this.likelist.concat(res.data.list);
						if (this.likelist.length == 0) {
							this.liketitle = '还没有喜欢的作品哦'
						}
					} else if (target_type == 1) {
						this.collist = this.collist.concat(res.data.list);
						if (this.collist.length == 0) {
							this.colltitle = '还没有收藏作品呢'
						}
					}
					if (res.code != 0) {
						this.liketitle = '还没有喜欢的作品哦'
						this.colltitle = '还没有收藏作品呢'
					}

					// this.$storage.setStorageSync('agetvideo', this.list)
				}).catch(err => {
					this.liketitle = '还没有喜欢的作品哦'
					this.colltitle = '还没有收藏作品呢'
					if (err.msg) {
						uni.showToast({
							icon: 'none',
							title: err.msg
						})
					}
					// console.log(err);
				})
			},
			follow() {
				if (this.$store.state.user.accessToken === '' || this.$store.state.user.accessToken ===
					null) {
					this.$store.dispatch('user/accessToken');
					return;
				}
				if (this.user_id != this.userInfo.options.user_id) {
					return
				}
				uni.navigateTo({
					url: '/pages/sixty/follow/follow?user_id=' + this.user_id
				})
			},
			fans() {
				if (this.$store.state.user.accessToken === '' || this.$store.state.user.accessToken ===
					null) {
					this.$store.dispatch('user/accessToken');
					return;
				}
				if (this.user_id != this.userInfo.options.user_id) {
					return
				}
				uni.navigateTo({
					url: '/pages/sixty/fans/fans?user_id=' + this.user_id
				})
			},
			initLoad() {
				this.$showLoading();
				this.$request({
					url: this.$api.sixty.mine,
					data: {
						user_id: this.user_id
					}
				}).then((res) => {
					this.$hideLoading();
					this.vidianinfo = res.data.vidianinfo
					if (this.vidianinfo.description) {
						this.vidianinfo.description = this.vidianinfo.description.replaceAll('<p>', '').replaceAll(
							'</p>', '')
					}
					this.uInfo = res.data.userInfo
					this.fansnum = res.data.fansNumb
					this.foucesnum = res.data.focusedNumb
					this.is_focus = res.data.is_focus
					this.is_own = res.data.is_own
					if (res.data.userInfo.userInfo.gender == 0) {
						this.uInfo.sex = '未知'
					} else if (res.data.userInfo.userInfo.gender == 1) {
						this.uInfo.sex = '男'
					} else if (res.data.userInfo.userInfo.gender == 2) {
						this.uInfo.sex = '女'
					}
					if (res.data.is_own != 1) {
						uni.setNavigationBarTitle({
							title: res.data.userInfo.nickname + '的主页'
						})
					}
				}).catch(err => {
					if (err.msg) {
						uni.showToast({
							icon: 'none',
							title: err.msg
						})
					}
					this.$hideLoading();
					// console.log(err);
				})
			},
			// 10.22 添加编辑
			edit() {
				uni.navigateTo({
					url: '/pages/sixty/user-center/editinfo'
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.user-top {
		background-color: #FFFFFF;
		// padding: 0 32rpx;

		.nickname {
			font-size: 32rpx;
			font-weight: 600;
			line-height: 60rpx;
			padding: 0 32rpx;
		}

		.introduce {
			font-size: 24rpx;
			line-height: 42rpx;
			padding: 0 32rpx;
		}

		// 10.22 添加
		.in-text {
			margin-right: 28rpx;
		}

		// 编辑
		.edit {
			border: 1rpx solid #CDCDCD;
			display: inline-block;
			padding: 10rpx 24rpx;
		}

		.info {
			display: flex;
			font-size: 24rpx;
			line-height: 60rpx;
			padding: 0 32rpx;

			.gender {}

			.address {
				padding: 0 20rpx;
			}
		}
	}

	.top {
		width: 686rpx;
		margin: 0 auto;
		padding: 32rpx 0;
		display: flex;
		justify-content: space-between;
		align-items: center;

		.avatar {
			width: 150rpx;
			height: 150rpx;
			border-radius: 50%;
			// 头像加黑圈？9.9
			border: 6rpx solid #000;

			.img {
				width: 100%;
				height: 100%;
				border-radius: 50%;
			}
		}

		.user {
			display: flex;
			align-items: center;
			text-align: center;

			.follow {
				width: 160rpx;
				height: 120rpx;
				line-height: 120rpx;
				font-size: 28rpx;

				.text {
					font-size: 38rpx;
					padding-right: 8rpx;
				}
			}

			.fans {
				width: 160rpx;
				height: 120rpx;
				line-height: 120rpx;
				font-size: 28rpx;

				.text {
					font-size: 38rpx;
					padding-right: 8rpx;
				}
			}
		}

		.attention {
			width: 320rpx;
			text-align: center;
			font-size: 24rpx;
			font-weight: 600;
			line-height: 60rpx;

			&.addten {
				background: #fa2424;
				color: #FFFFFF;
			}

			&.remten {
				background: #e7e7e7;
			}
		}
	}

	.three {}

	.threefix {
		position: fixed;
		width: 100%;
		background-color: #FFFFFF;
		top: 0;
		left: 0;
		z-index: 999;
	}

	.flex {
		display: flex;
		justify-content: space-around;
		align-items: center;
		padding: 24rpx 0;
		height: 120rpx;

		.sitem {
			height: 72rpx;
			line-height: 72rpx;
			font-size: 28rpx;

			.seaimg {
				width: 36rpx;
				height: 36rpx;
				margin: 18rpx auto;
			}
		}

		.fitem {
			margin-left: 20rpx;
			color: #ff2600;
		}

		.item {
			width: 100rpx;
			height: 72rpx;
			line-height: 72rpx;
			position: relative;
			text-align: center;
			font-size: 28rpx;

			&.active:before {
				position: absolute;
				bottom: 0;
				left: 0;
				content: '';
				font-weight: 500;
				background: #342B2A;
				border-radius: 4rpx;
				width: 80rpx;
				left: 50%;
				transform: translateX(-50%);
				height: 8rpx;
			}
		}
	}

	.nocenter {
		width: 100%;
		text-align: center;
		line-height: 120rpx;
		font-size: 28rpx;
	}
</style>
